//
//  FileManagerController.swift
//  ExpressDriver
//
//  Created by sheng on 2017/11/15.
//  Copyright © 2017年 sheng. All rights reserved.
//

import UIKit
import SnapKit
import MJRefresh

class FileManagerController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var fileTableView: UITableView!
    
    let freshHeader = MJRefreshNormalHeader()

    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    
    
    func setUI() {
        fileTableView = UITableView();
        fileTableView.delegate = self
        fileTableView.dataSource = self
        fileTableView.showsVerticalScrollIndicator = false
        let nib = UINib(nibName: String(describing: OrderFileCellTableViewCell.self), bundle: nil)
        fileTableView.register(nib, forCellReuseIdentifier: "fileItem")
        fileTableView.estimatedRowHeight = 120
        fileTableView.rowHeight = UITableViewAutomaticDimension
        self.view.addSubview(fileTableView)
        
        fileTableView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalTo(self.view)
        }
        
        freshHeader.setTitle("Pull to refresh", for: .idle)
        freshHeader.setTitle("Release to refresh", for: .pulling)
        freshHeader.setTitle("Loading...", for: .refreshing)
        //header.lastUpdatedTimeLabel.isHidden = true
        freshHeader.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        fileTableView!.mj_header = freshHeader
    }
    
    
    func headerRefresh(){
        print("下拉刷新.")
        sleep(2)
        //重现加载表格数据
        fileTableView!.reloadData()
        //结束刷新
        fileTableView!.mj_header.endRefreshing()
    }
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "fileItem", for: indexPath) as! OrderFileCellTableViewCell
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print("第\(indexPath.row)行被点击了")
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    

}
